import Vue from "vue";
import Vuex from "vuex";
import types from "./types";
import userStore from "./modules/userStore";
import dataStore from "./modules/permissionStore";
import selectStore from "./modules/selectStore";
import envStore from "./modules/envStore";
import {URLS} from "../api/api";

Vue.use(Vuex);

// 实现自动注册loading URL
const loading = {};
Object.values(URLS).forEach(value => {
  loading[value] = false;
});

export default new Vuex.Store({
  strict: true,
  modules: {
    userStore,
    dataStore,
    selectStore,
    envStore
  },
  state: {
    isLoading: false,
    collapsed: false,
    // url请求列表，自动注册
    loading: loading
  },
  getters: {
  },
  mutations: {
    loading(state, url){
      state.loading[url] = true;
    },
    unloading(state, url){
      state.loading[url] = false;
    },
    [types.LOADING](state, payload) {
      if (payload) {
        state.isLoading = payload;
      } else {
        state.isLoading = false;
      }
    },
    [types.TOGGLE_COLLAPSED] (state) {
      state.collapsed = !state.collapsed;
    },
    [types.SET_BREADCRUMB_ITEMS] (state, items) {
      state.breadcrumbItems = items;
    }
  },
  actions: {

  }
});
